<?php
namespace Modules\Products\Controllers;

class IndexController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute() {
		\Components\Utils::isClosedFrontent($this);
	}

	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function indexAction() {
		$query = $this -> request -> getQuery();
		if(!isset($query['page'])) {
			$query['page'] = 1;
		} else if($query['page'] <= 0)  {
			$query['page'] = 1;
		} else {
			$query['page'] = abs($query['page']);
		}

		if(!isset($query['limit'])) {
			$query['limit'] = 10;
		} else if($query['limit'] <= 0)  {
			$query['limit'] = 10;
		} else {
			$query['limit'] = abs($query['limit']);
		}

		$params = [
			'order' => 'products_id DESC'
		];
		if(isset($query['is_shelf'])) {
			$params['conditions'] = 'is_shelf = ' . $query['is_shelf'];
		}

		$service = $this -> products::getServiceInstance() -> pager($params, $query['limit'], $query['page']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['pager']]);
	}

	public function findByIdsAction() {
		$query = $this -> request -> getQuery();
		if(!isset($query['ids']) || empty($query['ids'])) {
			return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => []]);
		}

		$ids = '';
		foreach($query['ids'] as $v) {
			$ids .= $v . ',';
		}
		$ids = rtrim($ids, ',');
		$service = $this -> products::getServiceInstance() -> find([
			'conditions' => 'products_id in(' . $ids . ')'
		]);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['productses']]);
	}

	public function findFirstAction($products_id) {
		$service = $this -> products::getServiceInstance() -> findFirstById($products_id);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success','products'=>$service['products']]);
	}

	public function createAction() {
		$validation = $this -> products::getValidationInstance()
		-> addProductsClassId()
		-> addName()
		-> addIsShelf()
		-> execute($this -> request -> getPost());
		if($validation['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$validation['code'],'msg'=>$validation['message']]);
		}

		$filter = $this -> products::getFilterInstance()
		-> setData($validation['data'])
		-> addProductsClassId()
		-> addName()
		-> addImages()
		-> addPrice()
		-> addStock()
		-> addDetails()
		-> addIsShelf()
		-> addCreateTime()
		-> addLastUpdateTime()
		-> execute();
		if($filter['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$filter['code'],'msg'=>$filter['message']]);
		}

		$service = $this -> products::getServiceInstance() -> create($filter['data']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'创建成功','filter'=>$filter['data']]);
	}

	public function updateAction($products_id) {
		$validation = $this -> products::getValidationInstance()
		-> addProductsClassId()
		-> addName()
		-> addIsShelf()
		-> execute($this -> request -> getPut());
		if($validation['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$validation['code'],'msg'=>$validation['message']]);
		}

		$filter = $this -> products::getFilterInstance()
		-> setData($validation['data'])
		-> addProductsClassId()
		-> addName()
		-> addImages()
		-> addPrice()
		-> addStock()
		-> addDetails()
		-> addIsShelf()
		-> addCreateTime()
		-> addLastUpdateTime()
		-> execute();
		if($filter['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$filter['code'],'msg'=>$filter['message']]);
		}

		$service = $this -> products::getServiceInstance() -> update($products_id, $filter['data']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'修改成功']);
	}

	public function deleteAction($products_id) {
		$service = $this -> products::getServiceInstance() -> delete($products_id);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'删除成功']);
	}
}